{% extends 'base.html' %}
{% load static %}

{% block extra_css %}
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
{% endblock %}

{% block title %}{{ title }} - Allen监控系统{% endblock %}

{% block content %}
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title mb-0">{{ title }}</h3>
                </div>
                <div class="card-body">
                    <form method="post" id="assetForm">
                        {% csrf_token %}
                        {% for field in form %}
                        <div class="mb-3">
                            <label for="{{ field.id_for_label }}" class="form-label">{{ field.label }}</label>
                            <div class="input-group">
                                {{ field }}
                                {% if field.name == 'category' %}
                                <button type="button" class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#categoryModal">
                                    <i class="fas fa-plus"></i> 新增分类
                                </button>
                                {% endif %}
                                {% if field.name == 'responsible_person' %}
                                <button type="button" class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#personModal">
                                    <i class="fas fa-plus"></i> 新增负责人
                                </button>
                                {% endif %}
                            </div>
                            {% if field.errors %}
                            <div class="invalid-feedback d-block">
                                {{ field.errors }}
                            </div>
                            {% endif %}
                            {% if field.help_text %}
                            <div class="form-text">{{ field.help_text }}</div>
                            {% endif %}
                        </div>
                        {% endfor %}
                        <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                            <a href="{% url 'asset_management' %}" class="btn btn-secondary me-md-2">取消</a>
                            <button type="submit" class="btn btn-primary">保存</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 新增分类模态框 -->
<div class="modal fade" id="categoryModal" tabindex="-1" aria-labelledby="categoryModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="categoryModalLabel">新增资产分类</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="categoryForm">
                    <div class="mb-3">
                        <label for="categoryName" class="form-label">分类名称</label>
                        <input type="text" class="form-control" id="categoryName" name="name" required>
                    </div>
                    <div class="mb-3">
                        <label for="categoryDescription" class="form-label">描述</label>
                        <textarea class="form-control" id="categoryDescription" name="description" rows="3"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="saveCategoryBtn">保存</button>
            </div>
        </div>
    </div>
</div>

<!-- 新增负责人模态框 -->
<div class="modal fade" id="personModal" tabindex="-1" aria-labelledby="personModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="personModalLabel">新增负责人</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="personForm">
                    <div class="mb-3">
                        <label for="personName" class="form-label">姓名</label>
                        <input type="text" class="form-control" id="personName" name="name" required>
                    </div>
                    <div class="mb-3">
                        <label for="personPhone" class="form-label">联系电话</label>
                        <input type="text" class="form-control" id="personPhone" name="phone" required>
                    </div>
                    <div class="mb-3">
                        <label for="personEmail" class="form-label">邮箱</label>
                        <input type="email" class="form-control" id="personEmail" name="email">
                    </div>
                    <div class="mb-3">
                        <label for="personDepartment" class="form-label">部门</label>
                        <input type="text" class="form-control" id="personDepartment" name="department" required>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="savePersonBtn">保存</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
$(document).ready(function() {
    // 初始化 Select2，禁用搜索功能
    $('#id_category').select2({
        theme: 'bootstrap-5',
        minimumResultsForSearch: Infinity
    });
    $('#id_responsible_person').select2({
        theme: 'bootstrap-5',
        minimumResultsForSearch: Infinity
    });

    // 保存新分类
    $('#saveCategoryBtn').click(function() {
        const formData = new FormData($('#categoryForm')[0]);
        $.ajax({
            url: '{% url "asset_category_create" %}',
            method: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            headers: {
                'X-CSRFToken': '{{ csrf_token }}'
            },
            success: function(response) {
                if (response.status === 'success') {
                    // 添加新选项到分类下拉框
                    const newOption = new Option(response.name, response.id, true, true);
                    $('#id_category').append(newOption).trigger('change');
                    
                    // 关闭模态框并重置表单
                    $('#categoryModal').modal('hide');
                    $('#categoryForm')[0].reset();
                }
            },
            error: function(xhr) {
                alert('保存失败：' + xhr.responseJSON.errors);
            }
        });
    });

    // 保存新负责人
    $('#savePersonBtn').click(function() {
        const formData = new FormData($('#personForm')[0]);
        $.ajax({
            url: '{% url "responsible_person_create" %}',
            method: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            headers: {
                'X-CSRFToken': '{{ csrf_token }}'
            },
            success: function(response) {
                if (response.status === 'success') {
                    // 添加新选项到负责人下拉框
                    const newOption = new Option(response.name, response.id, true, true);
                    $('#id_responsible_person').append(newOption).trigger('change');
                    
                    // 关闭模态框并重置表单
                    $('#personModal').modal('hide');
                    $('#personForm')[0].reset();
                }
            },
            error: function(xhr) {
                alert('保存失败：' + xhr.responseJSON.errors);
            }
        });
    });
});
</script>
{% endblock %} 